﻿using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSY.SerialCommunication.UpperComputer.Core.AOP.Autofac
{
    /// <summary>
    /// Autoafac
    /// </summary>
    public class LogAutofacInterceptor : Castle.DynamicProxy.IInterceptor
    {
        //public void Intercept(IInvocation invocation)
        //{
        //    Console.WriteLine($"Entering method {invocation.Method.Name}");
        //    invocation.Proceed();
        //    Console.WriteLine($"Exiting method {invocation.Method.Name}");
        //}
        public void Intercept(IInvocation invocation)
        {
            // 前置处理
            Console.WriteLine($"Before method: {invocation.Method.Name}");
            Console.WriteLine($"Arguments: {string.Join(", ", invocation.Arguments)}");

            try
            {
                // 调用实际方法
                invocation.Proceed();

                // 成功返回后处理
                Console.WriteLine($"Method {invocation.Method.Name} executed successfully");
            }
            catch (Exception ex)
            {
                // 异常处理
                Console.WriteLine($"Exception in {invocation.Method.Name}: {ex.Message}");
                throw;
            }
            finally
            {
                // 后置处理
                Console.WriteLine($"After method: {invocation.Method.Name}");
            }
        }
    }
}
